Repository navigation
Add instrumentation plugin API to Amazon.Lambda.DurableExecution (groundwork for OpenTelemetry) - #2611
Draft
normj wants to merge 9 commits into
Draft
Add instrumentation plugin API to Amazon.Lambda.DurableExecution (groundwork for OpenTelemetry)#2611normj wants to merge 9 commits into
normj wants to merge 9 commits into
Conversation
Adds IDurableInstrumentationPlugin, its factory, the info types and DurableExecutionOptions.Plugins, plus an internal PluginRunner that dispatches hooks with per-plugin exception isolation, ordered start/end, a WrapAsync chain that always runs the user code exactly once, first-wins trace propagation and a bounded OnInvocationEndAsync. Not wired into DurableFunction yet.
Adds WrapAsync overloads that take DurableExecutionOptions. A plugin is created per invocation, gets the invocation start and end hooks and wraps the workflow function. The invocation ends as Succeeded, Failed, Pending, or Retrying when the invocation throws. InvocationInfo carries the trace header from ILambdaContext.TraceId and the operations the service reports as updated (UpdatedOperationIds). With no plugins registered the code path is unchanged.
…gins DurableOperation.ExecuteAsync reports OperationStart when the workflow reaches an operation and OperationEnd when it reaches a terminal state (or is replayed as terminal). Operations that suspend, or are cancelled, report no end. Steps and wait-for-condition polls report AttemptStart/AttemptEnd and run their user function inside the plugins' WrapAsync; child contexts wrap their function the same way. Map, parallel, incremental parallel and their branches carry the plugins too. OperationInfo.Attempt is the 1-based latest attempt (the service records the number of earlier attempts). Operations without plugins take the unchanged code path.
The durable logger adds the fields a plugin returns from GetLogScope to the scope of every entry, read per entry because they follow the active span. A chained invoke asks the plugins for a trace header after the payload is serialized and writes it to ChainedInvokeOptions.XAmznTraceId on the START checkpoint; an invoke that is replayed does not ask again. Raises the AWSSDK.Lambda minimum to 4.0.110.1, the first version with that field.
…nner Operations that a checkpoint response adds or whose status it changes (a callback completed by an external system, a timer that fired) are reported through OnOperationChange with a snapshot of every known operation. A response that repeats a known status reports nothing. TestRunnerOptions.ExecutionOptions passes DurableExecutionOptions to every invocation of a local test run so plugins can be tested end to end.
Found by running a plugin against real durable executions in us-west-2. - StepDetails.Attempt counts finished attempts. A finished step record already holds the number of its last attempt (SUCCEEDED on the second try reports 2), while a ready or pending one is on the attempt after it. The attempt number was one too high on finished records. - A callback is registered long before it finishes. Its operation now ends when the result is read (replayed from history, failed or timed out), not when CreateCallbackAsync returns the handle. - StartTimestamp and EndTimestamp were not copied from checkpoint responses and state pages, so operations that finished in the current invocation had no service timestamps. They are mapped now.
Adds a user guide for plugin authors (registration, hooks, guarantees, what the service reports, testing) and a design note with the decisions behind the hook contract. Links them from the README, the Testing README and CLAUDE.md, which lists the rules to keep when changing the plugin code.
Found by tracing a real execution: a step that was retried reached OnOperationStart as a fresh start in the invocation that ran its second attempt, so a plugin that derives an id from the operation id used it twice. IsReplay on OperationStart is now true whenever the operation already has a checkpoint when the workflow reaches it, not only when it had finished. That covers a step resumed after a retry and a child context that suspended. On OperationEnd it still means the operation had already finished, so the resumed step's end is not a replay.
normj
force-pushed
the
normj/durable-otel-plugin-api
branch
from
November 18, 2026 23:41
ff53240 to
4ddfe68
Compare
Read from ILambdaContext.W3C, which carries clientContext.w3c. Lets a plugin extract a W3C trace context the way it already can an X-Ray one.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue #, if available:
Description of changes:
Draft. Opened early so others can follow the work. The core plugin API in this PR is complete; the pieces after it are separate PRs.
Background
The Python, JavaScript and Java durable execution SDKs added OpenTelemetry support in Core 3.0 / OTel 2.0. In all three, the core SDK has a generic instrumentation plugin framework with no knowledge of tracing, and OTel is one plugin built on it. This PR adds the same framework to
Amazon.Lambda.DurableExecution.What this PR adds
Amazon.Lambda.DurableExecution.Instrumentation:IDurableInstrumentationPlugin(every hook optional),IDurableInstrumentationPluginFactory(a new plugin per invocation, optionalExclusiveGroup), info types,PluginLoadException, andDurableExecutionOptions.Plugins.DurableFunction.WrapAsyncoverloads that takeDurableExecutionOptions. Existing signatures are unchanged, and with no plugin registered the code path is unchanged.OnOperationChangefor operations a checkpoint response adds or changes, andWrapAsyncaround the invocation, each attempt and each child context. Map, parallel and incremental parallel and their branches are covered.ChainedInvokeOptions.XAmznTraceIdon the invoke START. This raises theAWSSDK.Lambdaminimum from 4.0.13.1 to 4.0.110.1, the first version with that field.GetLogScopeto the scope of every entry.TestRunnerOptions.ExecutionOptionsruns plugins in the local test runner.Contract
WrapAsyncalways runs the user code exactly once and lets its exception through.OnInvocationEndAsync, which is bounded by the remaining invocation time.Checked against a real durable execution (us-west-2)
I ran a plugin that logs every hook in a deployed function (a step that fails once and retries, a wait, a child context, a parallel, a chained invoke, a terminal step failure, and a callback completed externally). That test function is not part of this PR. It confirmed:
ILambdaContext.TraceIdis the same on every invocation of an execution and equals_X_AMZN_TRACE_ID.UpdatedOperationIdsis sent by the service and parses correctly.XAmznTraceIdon a chained invoke and delivers it to the invoked function.StepDetails.Attemptcounts finished attempts, so a finished record already holds its last attempt number. I had it one too high on finished records; fixed in this PR.CreateCallbackAsyncreturns. Also fixed.IsReplayfalse) by the invocation that ran its second attempt.IsReplayat start is now true whenever the operation already has a checkpoint; at end it still means the operation had already finished. Found while tracing a real execution with the OpenTelemetry plugin ([Draft, stacked on #2611] Add the OpenTelemetry tracing plugin (Execution and Invocation views) #2612).StartTimestamp/EndTimestampwere dropped when mapping checkpoint responses, so operations that finished in the current invocation had no service timestamps. They are mapped now.Not checked: Lambda Managed Instances, and the behavior of a very large number of operations.
Stack
This is the bottom of a stack of draft PRs:
Amazon.Lambda.DurableExecution.OpenTelemetry, the OTel plugin, usingSystem.Diagnostics.ActivitySource(no OpenTelemetry NuGet dependency).[DurableExecution(Tracing = ..., PluginProvider = ...)].Not yet done: the shared
pluginconformance suite (10-1 to 10-23) for this API.Out of scope for now: the Durable Execution Telemetry exporter.
Testing
Amazon.Lambda.DurableExecution.Tests(629) andAmazon.Lambda.DurableExecution.Testing.Tests(156) pass on net8.0 and net10.0. New tests cover the runner, invocation wiring, operation and attempt hooks, callbacks, change notifications, log scope, propagation, attempt numbering and timestamp mapping. For the behaviors that matter most I checked that the tests fail when the behavior is removed.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.